using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace Microsoft.Office.Project.EPMSync.Config.Library
{
    public static class RightAwareMessageBox
    {
        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            if (IsRightToLeft(null))
                return MessageBox.Show(text, caption, buttons, icon, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign);
            else
                return MessageBox.Show(text, caption, buttons, icon);
        }

        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            if (IsRightToLeft(owner))
                return MessageBox.Show(owner, text, caption, buttons, icon, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign);
            else
                return MessageBox.Show(owner, text, caption, buttons, icon);
        }

        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            if (IsRightToLeft(null))
                return MessageBox.Show(text, caption, buttons, icon, defaultButton, MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign);
            else
                return MessageBox.Show(text, caption, buttons, icon, defaultButton);
        }

        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            if (IsRightToLeft(owner))
                return MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign);
            else
                return MessageBox.Show(owner, text, caption, buttons, icon, defaultButton);
        }

        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
        {
            if (IsRightToLeft(null))
                options |= MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign;

            return MessageBox.Show(text, caption, buttons, icon, defaultButton, options);
        }

        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
        {
            if (IsRightToLeft(owner))
                options |= MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign;

            return MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, options);
        }

        private static bool IsRightToLeft(IWin32Window owner)
        {
            Control control = owner as Control;

            if (control != null)
                return control.RightToLeft == RightToLeft.Yes;
            else
                return CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft;
        }

    }

}
